package com.inspectortime.mail;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.inspectortime.domain.EmailTemplate;
import com.inspectortime.domain.Inspection;
import com.inspectortime.mail.service.AbstractMailMessage;
import com.inspectortime.mail.service.DynamicMailMessage;
import com.inspectortime.mail.service.StaticMailMessage;
import com.inspectortime.mail.service.StaticMailMessageTemplateNames;

public class EmailMessageUtils {
	
	public static AbstractMailMessage prepareBuyerConfirmationMessage(Inspection inspection, HttpServletRequest request) {
		
		Map<String, Object> props = new HashMap<String, Object>();
		props.put("inspection", inspection);
		String toEmail = inspection.getHomeBuyer().getEmail();
		AbstractMailMessage message = null;
		EmailTemplate emailTemplate = getEmailTemplate(EmailTemplate.HOME_BUYER_CONFIRMATION_TEMPLATE, inspection);
		if (emailTemplate != null) {
			// Use custom template from DB
			message = new DynamicMailMessage(emailTemplate, inspection, request, toEmail);
		} else {
			// Inspector company has not set up a custom template. Send the default message from file.
			String subject ="Your Home Inspection Confirmation";
			message = new StaticMailMessage(StaticMailMessageTemplateNames.INSPECTION_CONFIRMATION_FOR_HOME_BUYER, props, toEmail, subject);
		}
		
		/*if (inspection.getInspector().getCompany().getEmailTemplates().isEmpty()) {
			
			// Inspector company has not set up a custom template. Send the default message from file.
			String subject ="Your Home Inspection Confirmation";
			message = new StaticMailMessage(StaticMailMessageTemplateNames.INSPECTION_CONFIRMATION_FOR_HOME_BUYER, props, toEmail, subject);
		} else {
			
			// Use custom template from DB
			// TODO: Note we'll want to change this when we add multiple dynamic templates for each company.  But this will do for now.
			EmailTemplate emailTemplate = inspection.getInspector().getCompany().getEmailTemplates().iterator().next();	
			message = new DynamicMailMessage(emailTemplate, inspection, request, toEmail);
		}*/
		message.setReplyTo(inspection.getInspector().getEmail());
		
		return message;
	}
	
	public static AbstractMailMessage prepareInspectorConfirmationMessage(Inspection inspection, HttpServletRequest request) {
		
		Map<String, Object> props = new HashMap<String, Object>();
		props.put("inspection", inspection);
		String toEmail = inspection.getInspector().getEmail();
		String subject = "[InspectorTime] You have a new scheduled inspection";
		AbstractMailMessage message = new StaticMailMessage(StaticMailMessageTemplateNames.INSPECTION_CONFIRMATION_FOR_INSPECTOR, props, toEmail, subject);
		return message;
	}
	
	
	public static AbstractMailMessage prepareRealtorThankYouMessage(Inspection inspection, HttpServletRequest request) {
		
		Map<String, Object> props = new HashMap<String, Object>();
		props.put("inspection", inspection);
		String toEmail = inspection.getReferringAgent().getEmail();
		AbstractMailMessage message = null;
		EmailTemplate emailTemplate = getEmailTemplate(EmailTemplate.REALTOR_THANK_YOU_TEMPLATE, inspection);
		
		if (emailTemplate != null) {
			// Use custom template from DB
			message = new DynamicMailMessage(emailTemplate, inspection, request, toEmail);
		} else {
			// Inspector company has not set up a custom template. Send the default message from file.
			String subject = "[InspectorTime] Thank you";
			message = new StaticMailMessage(StaticMailMessageTemplateNames.REALTOR_THANK_YOU, props, toEmail, subject);
		}
		message.setReplyTo(inspection.getInspector().getEmail());
		
		return message;
	}
	
	
	private static EmailTemplate getEmailTemplate(String templateName, Inspection inspection) {
		EmailTemplate template = null;
		if (!inspection.getInspector().getCompany().getEmailTemplates().isEmpty()) {
			for (EmailTemplate tempTemplate : inspection.getInspector().getCompany().getEmailTemplates()) {
				if (templateName.equals(tempTemplate.getName())) {
					template = tempTemplate;
					break;
				}
			}
		}
		
		return template;
	}

}
